<?php

namespace App\Http\Controllers;

use App\Http\Resources\BaseResource;
use App\Http\Resources\ErrorResource;
use App\Repositories\Contracts\TcPolicyInterface;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseCollection;
use MakeRep\Services\ParamService;

class TcPolicyController extends BaseController
{
    public function __construct(TcPolicyInterface $interface)
    {
        parent::__construct($interface);
    }

    /**
     * 上传政策
     */
    public function ajaxAdd(Request $request)
    {
        $res = $this->interface->create($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 列表
     * @param Request $request
     * @return BaseCollection
     */
    public function ajaxLists(Request $request){

        $pageSize = $request->page_size ? : 10;
        $orderBy = $request->order_by ? : 'desc';
        if (in_array(40,$request->admin_platform)) {
            $where = ParamService::createCondition($request, isset($this->interface->fillable) ? $this->interface->fillable : []);
            $where['platform'] = 40;
            $res = $this->interface->pageLists($where, '*', $pageSize, 'id', $orderBy);

            foreach ($res as $key => &$value) {
                $policyId = $value['policy_id'];
                if (!empty($policyId)) {
                    $tcPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface');
                    $tcPolicyInfo = $tcPolicyInterface->getPolicyInfoById($policyId);
                    if ($tcPolicyInfo) {
                        if ($tcPolicyInfo['data']) {
                            $policy = $tcPolicyInfo['data'][0];
                            if ($policy) {
                                $value['seat'] = $policy['everydayInventory'] . '/' . $policy['everydayInventory'];
                            }
                        }
                    }
                } else {
                    $value['seat'] = $value['seat'] . '/' . $value['seat'];
                }
            }
            return new BaseCollection($res);
        }
        return new BaseCollection(new LengthAwarePaginator([],0,10));
    }

    /**
     * 转到详情页面
     */
    public function update(Request $request){

        $where = $request->id ? ['id'=>$request->id,'admin_id'=>request()->admin['id']] : ['admin_id'=>request()->admin['id']];
        $res = $this->interface->index($where);
        if ($res) {
            $policyId = $res['policy_id'];
            $tcPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcPolicyInterface');
            $tcPolicyInfo = $tcPolicyInterface->getPolicyInfoById($policyId);
            if ($tcPolicyInfo) {
                if($tcPolicyInfo['data']) {
                    $policy = $tcPolicyInfo['data'][0];
                    if ($policy) {
                        $policy['id'] = $res['id'];
                        $policyInfo = ['policy' => $policy];
                        return view('tcpolicy/update', $policyInfo);
                    }
                }
            }
        }
        abort(404);
    }


    /**
     * 更新政策
     */
    public function ajaxUpdate(Request $request){
        $res = $this->interface->updatePolicy($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }
        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 删除政策
     */
    public function ajaxDelete(Request $request){
        $res = $this->interface->delPolicy($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }
        return new ErrorResource(['errmsg'=>$res]);
    }

}